﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using BusinessObjects;
using DataAccess;

namespace QuanLyHopDong
{
    public partial class HopDongLaoDongDetailView : DevExpress.XtraEditors.XtraForm
    {
        public event EventHandler SaveChanged;

        private HopDongLaoDong _HopDongLaoDong;
        private DieuKhoanHopDong _DieuKhoanHopDong;
        private BindingList<HopDongLaoDong> bindingListHopDongLaoDong;
        private BindingList<DieuKhoanHopDong> bindingListDieuKhoanHopDong;
        private BindingList<NhanVien> blNhanVien;
        private BindingList<ChucVu> blChucVu;
        private BindingList<NhanVien> blHoSo;
        private BindingList<BoPhan> blBoPhan;
        private BindingList<HinhThucHopDong> blHinhThucHopDong;
        private BindingList<NgachLuong> blNgachLuong;
        private BindingList<BacLuong> blBacLuong;

        public HopDongLaoDongDetailView(HopDongLaoDong hopDongLaoDong)
        {
            InitializeComponent();

            _HopDongLaoDong = new HopDongLaoDong();
            _HopDongLaoDong = hopDongLaoDong;
            _DieuKhoanHopDong = new DieuKhoanHopDong();

            if (_HopDongLaoDong.Oid>0)
                _DieuKhoanHopDong = DataAccessObject.DieuKhoanHopDong.GetDieuKhoanHopDong(_HopDongLaoDong.Oid);

            //\\ Load danh sách nhân viên.
            IList<NhanVien> lsNhanVien = DataAccessObject.NhanVien.GetListNhanVien();
            blNhanVien = new BindingList<NhanVien>(lsNhanVien);
            nhanVienBindingSource.DataSource = blNhanVien;

            //\\ Load danh sách chức vụ.
            blChucVu = new BindingList<ChucVu>(DataAccessObject.ChucVu.GetListChucVu());
            chucVuBindingSource.DataSource = blChucVu;

            //\\ Load danh sách hồ sơ.
            blHoSo = new BindingList<NhanVien>(lsNhanVien);
            hoSoBindingSource.DataSource = blNhanVien;

            //\\ Load danh sách bộ phận.
            blBoPhan = new BindingList<BoPhan>(DataAccessObject.BoPhan.GetListBoPhan());
            boPhanBindingSource.DataSource = blBoPhan;

            //\\ Load danh sách hình thức hợp đồng.
            blHinhThucHopDong = new BindingList<BusinessObjects.HinhThucHopDong>(DataAccessObject.HinhThucHopDong.GetListHinhThucHopDong());
            hinhThucHopDongBindingSource.DataSource = blHinhThucHopDong;

            //\\ Load danh sách ngạch lương.
            blNgachLuong = new BindingList<BusinessObjects.NgachLuong>(DataAccessObject.NgachLuong.GetListNgachLuong());
            ngachLuongBindingSource.DataSource = blNgachLuong;

            //\\ Load danh sách bậc lương.
            blBacLuong = new BindingList<BusinessObjects.BacLuong>(DataAccessObject.BacLuong.GetListBacLuong());
            bacLuongBindingSource.DataSource = blBacLuong;

            //\\ Add hợp đồng làm việc vào BindingSource.
            bindingListHopDongLaoDong = new BindingList<HopDongLaoDong>();
            bindingListHopDongLaoDong.Add(_HopDongLaoDong);

            dsHopDongLaoDong.DataSource = bindingListHopDongLaoDong;

            //\\ Add điều khoản hợp đồng vào BindingSource.
            bindingListDieuKhoanHopDong = new BindingList<DieuKhoanHopDong>();
            bindingListDieuKhoanHopDong.Add(_DieuKhoanHopDong);
            dieuKhoanHopDongBindingSource.DataSource = bindingListDieuKhoanHopDong;

            if (_HopDongLaoDong.Oid != -1)
            {
                SetControlValues(_HopDongLaoDong);
            }

            layoutControl1.Refresh();
        }

        void OnSaveChange()
        {
            if (SaveChanged != null)
                SaveChanged(this, EventArgs.Empty);
        }

        private void btnLuuVaDong_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (_HopDongLaoDong.Oid == -1)
            {
                //\\ Insert điều khoản hợp đồng.
                if (DataAccessObject.DieuKhoanHopDong.Insert(_DieuKhoanHopDong) > 0)
                {
                    _HopDongLaoDong.DieuKhoanHopDong = _DieuKhoanHopDong;
                    //\\ Insert hợp đồng làm việc.
                    DataAccessObject.HopDongLaoDong.Insert(_HopDongLaoDong);

                    OnSaveChange();
                }
            }
            else
            {
                //DataAccessObject.DieuKhoanHopDong.Update(_DieuKhoanHopDong);
                DataAccessObject.HopDongLaoDong.Update(_HopDongLaoDong);
            }
            this.Close();
        }

        private void HopDongLamViecDetailView_Load(object sender, EventArgs e)
        {
            ////\\Bộ phận
            //string[] listFieldName = new string[] { "MaQuanLy", "TenBoPhan", "TrucThuoc.TenBoPhan" };
            //string[] listColumnName = new string[] { "Mã Quản Lý", "Tên Bộ Phận", "Trực Thuộc" };
            //AppGridView.InitGridView(DiaDiemLamViecView, false, false, DevExpress.XtraGrid.Views.Grid.GridMultiSelectMode.RowSelect, false, false, false, false);
            //AppGridView.ReadOnlyGridView(DiaDiemLamViecView);
            //AppGridView.ShowField(DiaDiemLamViecView, listFieldName, listColumnName);

            ////\\Người ký.
            //listFieldName = new string[] { "Ho", "Ten"};
            //listColumnName = new string[] { "Họ", "Tên"};
            //AppGridView.InitGridView(NguoiKyView, false, false, DevExpress.XtraGrid.Views.Grid.GridMultiSelectMode.RowSelect, false, false, false, false);
            //AppGridView.ReadOnlyGridView(NguoiKyView);
            //AppGridView.ShowField(NguoiKyView, listFieldName, listColumnName);

            ////\\Người lao động.
            //listFieldName = new string[] { "Ho", "Ten" };
            //listColumnName = new string[] { "Họ", "Tên" };
            //AppGridView.InitGridView(NguoiLaoDongView, false, false, DevExpress.XtraGrid.Views.Grid.GridMultiSelectMode.RowSelect, false, false, false, false);
            //AppGridView.ReadOnlyGridView(NguoiLaoDongView);
            //AppGridView.ShowField(NguoiLaoDongView, listFieldName, listColumnName);
        }

        private void ChucVuNguoiKy_EditValueChanged(object sender, EventArgs e)
        {
            _HopDongLaoDong.ChucVuNguoiKy = ChucVuNguoiKy.GetSelectedDataRow() as ChucVu;
        }

        private void NguoiKy_EditValueChanged(object sender, EventArgs e)
        {
            _HopDongLaoDong.NguoiKy = NguoiKy.GetSelectedDataRow() as NhanVien;
        }

        private void NguoiLaoDong_EditValueChanged(object sender, EventArgs e)
        {
            _HopDongLaoDong.NguoiLaoDong = NguoiLaoDong.GetSelectedDataRow() as NhanVien;
        }

        private void DiaDiemLamViec_EditValueChanged(object sender, EventArgs e)
        {
            _HopDongLaoDong.DiaDiemLamViec = DiaDiemLamViec.GetSelectedDataRow() as BoPhan;
        }

        private void HinhThucHopDong_EditValueChanged(object sender, EventArgs e)
        {
            _HopDongLaoDong.HinhThucHopDong = HinhThucHopDong.GetSelectedDataRow() as HinhThucHopDong;
        }

        private void cboPhanLoai_EditValueChanged(object sender, EventArgs e)
        {
            if (cboPhanLoai.SelectedIndex >= 0)
                _HopDongLaoDong.PhanLoai = (ELoaiHopDongLaoDong)cboPhanLoai.SelectedIndex;
        }

        private void NgachLuong_EditValueChanged(object sender, EventArgs e)
        {
            _DieuKhoanHopDong.NgachLuong = NgachLuong.GetSelectedDataRow() as NgachLuong;
        }

        private void BacLuong_EditValueChanged(object sender, EventArgs e)
        {
            _DieuKhoanHopDong.BacLuong = BacLuong.GetSelectedDataRow() as BacLuong;
        }

        private void btnDong_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.Close();
        }

        private void SetControlValues(HopDongLaoDong pHopDongLaoDong)
        {
            ChucVuNguoiKy.EditValue = pHopDongLaoDong.ChucVuNguoiKy.Oid;
            NguoiKy.EditValue = pHopDongLaoDong.NguoiKy.Oid;
            NguoiLaoDong.EditValue = pHopDongLaoDong.NguoiLaoDong.Oid;
            DiaDiemLamViec.EditValue = pHopDongLaoDong.DiaDiemLamViec.Oid;
            HinhThucHopDong.EditValue = pHopDongLaoDong.HinhThucHopDong.Oid;
            cboPhanLoai.EditValue = pHopDongLaoDong.PhanLoai;
            NgachLuong.EditValue = pHopDongLaoDong.DieuKhoanHopDong.NgachLuong.Oid;
            BacLuong.EditValue = pHopDongLaoDong.DieuKhoanHopDong.BacLuong.Oid;
        }
    }
}